﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Web.Question;
using PowerCreator.LMS.Question.Entiry;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Web.Paper;
using System.Text;
using PowerCreator.LMS.Common.Script;

public partial class sysadmin_QuestionManager_ManagerQuestionInfo : PowerCreator.LMS.Web.Base.BasePage
{

    ManagerPaper objManagerPaper = new ManagerPaper();
    ManagerQuestion objQuestion = new ManagerQuestion();
    Course objCourse = new Course();

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!GetRole(Role.ManagerQuestion))
        //{
        //    Script.ShowAndHistory("您没有管理试题的权限!", -1);
        //    return;
        //}
        if (!IsPostBack)
        {
            BinderDLL();
            string search = GetSearch();
            BinderPager(search);
            BinderGV(search, 1);
            

        }
    }
    protected override void InitPage()
    {
        lbl_InfoTitle.Text = GetLanageValueByKey("lab_ManagerPaperInfo_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
       
        lab_QuestionContent.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionContent", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_QuestonType.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionType", PowerCreator.LMS.Language.LanagetItemType.Page);

        btnSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
       
        btn_AddQuestion.Text = GetLanageValueByKey("btn_ManagerQuestionInfo_AddQuestion", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_DelCheck.Attributes.Add("onClick", "javascript:return confirm('" + GetLanageValueByKey("Msg_ManagerQuestion_DeletMsg", PowerCreator.LMS.Language.LanagetItemType.Page) + "');");

        base.InitPage();
    }
    private string GetSearch()
    {

        string QuestionTypeID = ddlQuestionTypeList.SelectedValue;
        string QuestionContent = txtQuestionContent.Text.Trim();


        StringBuilder search = new StringBuilder();
        //search.Append(" (QuestionIsFather=1 and QuestionClass=3) or QuestionClass<3  ");
        search.Append(" 1=1 and FKQuestionCourseID=-1 ");


        if (QuestionTypeID!="-1")
        {
            search.Append("  and  FKQuestionTypeID =" + QuestionTypeID);

        }
        if (!string.IsNullOrEmpty(QuestionContent))
        {
            search.Append("  and  QuestionContent like '%" + QuestionContent + "%'");

        }
        return search.ToString();
    }
    private void BinderPager(string search)
    {
        AspNetPager1.RecordCount = objQuestion.CountQuestion(search);
        AspNetPager1.PageSize = LMSPageSize;
    }

    private void BinderGV(string search, int pageIndex)
    {
        DataTable dt = new DataTable();
        dt = objQuestion.QueryQuestion(search, LMSPageSize, pageIndex);
        dt.Columns.Add("url");
        dt.Columns.Add("Index");
        dt.Columns.Add("QuestionText");
        dt.Columns.Add("CourseName");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i]["QuestionContent"] = StringUtils.GetSubstringLue(dt.Rows[i]["QuestionContent"].ToString().Trim(), 30);
            dt.Rows[i]["QuestionText"] = dt.Rows[i]["QuestionAnswer"].ToString().Trim().Replace("$", "") + StringUtils.GetSubstringLue(dt.Rows[i]["QuestionParse"].ToString().Trim(), 10);
            dt.Rows[i]["url"] = dt.Rows[i]["QuestionID"].ToString() + "$" + dt.Rows[i]["FKQuestionTypeID"].ToString(); //试题ID加试题类型ID
            dt.Rows[i]["Index"] = (pageIndex - 1) * 10 + i + 1;
            int _courseID = int.Parse(dt.Rows[i]["FKQuestionCourseID"].ToString());
            dt.Rows[i]["CourseName"] = objCourse.LoadVOTCourse(_courseID).CourseName;
        }
        gvQuestionList.DataSource = dt;
        gvQuestionList.DataBind();
    }
    private void BinderDLL()
    {

        ddlQuestionTypeList.DataSource = objQuestion.QueryQuestionType("");
        ddlQuestionTypeList.DataTextField = "QuestionTypeName";
        ddlQuestionTypeList.DataValueField = "QuestionTypeID";
        ddlQuestionTypeList.DataBind();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderGV(search, 1);
        //搜索
    }
    protected void gvQuestionList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string str = e.CommandArgument.ToString();
        int index = str.IndexOf("$");
        string questionID = str.Substring(0, index).Replace("$", "");

        string questionTypeID = str.Remove(0,index).Replace("$","");
        if (e.CommandName == "mydelete")
        {
            int qID = int.Parse(questionID);
            //删除前检查是否有试卷引用
            //if (!CheckQuestion_Del(qID))
            //{
            //    return;
            //}

            #region 删除题目表相关记录
            DataTable dt = new DataTable();
            dt = objQuestion.QueryQuestionOption("FKQuestionOptionQuestionID =" + qID);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int OptionID = int.Parse(dt.Rows[i]["QuestionOptionID"].ToString());
                objQuestion.DeleteQuestionOption(OptionID);
            }
            #endregion

            objQuestion.DeleteQuestion(qID);
            //string url = Utils.WebRoot + "/Message.aspx";
            //Script.OpenURL(url, "toolbar=no,location=no,fullscreen=no,personalbar=no,status=no,menubar=no,scrollbars=no,titlebar=no,resizeable=no,left=500,top=300,width=230,height=120");
            //Script.Alert(this,"msg",msg);
            //Script.ShowMsgRedirectUrl(msg, "ManagerQuestionInfo.aspx");

            string search = GetSearch();
            BinderPager(search);
            BinderGV(search, AspNetPager1.CurrentPageIndex);
        }
        if (e.CommandName == "myeditor")
        {
            string url = String.Empty;
            switch (questionTypeID)
            {
                case "1":
                    url = "EditQuestionInfo_panduan.aspx?qid=" + questionID + "&TypeID=" + questionTypeID ;
                    break;
                case "2":
                    url = "EditQuestionInfo_danxuan.aspx?qid=" + questionID + "&TypeID=" + questionTypeID;
                    break;
                case "3":
                    url = "EditQuestionInfo_duoxuan.aspx?qid=" + questionID + "&TypeID=" + questionTypeID;
                    break;
                case "4":
                    url = "EditQuestionInfo_tiank.aspx?qid=" + questionID + "&TypeID=" + questionTypeID;
                    break;
                case "5":
                    url = "EditQuestionInfo_jianda.aspx?qid=" + questionID + "&TypeID=" + questionTypeID;
                    break;
                case "6":
                    url = "EditQuestionInfo_jianda.aspx?qid=" + questionID + "&TypeID=" + questionTypeID;
                    break;
                case "7":
                    url = "EditQuestionInfo_zuhe.aspx?qid=" + questionID + "&TypeID=" + questionTypeID;
                    break;

            }
            
            Script.RedirectURL(url);
        }
    }
    protected void gvQuestionList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //gvQuestionList.Columns[2].Visible = false;
            LinkButton ibtnDelete = (LinkButton)e.Row.Cells[9].FindControl("ibtnDelete");
            ibtnDelete.Attributes.Add("onClick", "javascript:return confirm('" + GetLanageValueByKey("Msg_ManagerQuestion_DeletMsg", PowerCreator.LMS.Language.LanagetItemType.Page) + "');");

            e.Row.Attributes.Add("bgColor", "#F7FAFF");
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#FAEDB8'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#F7FAFF'");
            switch (e.Row.Cells[5].Text.Trim())
	        {
                case "1": e.Row.Cells[5].Text = GetLanageValueByKey("msg_CelueSelectQuest_panduanti", PowerCreator.LMS.Language.LanagetItemType.Page);
                    break;
                case "2": e.Row.Cells[5].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Danxuanti", PowerCreator.LMS.Language.LanagetItemType.Page);
                    break;
                case "3": e.Row.Cells[5].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Duoxuanti", PowerCreator.LMS.Language.LanagetItemType.Page);
                    break;
                case "4": e.Row.Cells[5].Text = GetLanageValueByKey("msg_CelueSelectQuest_tiankongti", PowerCreator.LMS.Language.LanagetItemType.Page);
                    break;
                case "5": e.Row.Cells[5].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Mingcijieshiti", PowerCreator.LMS.Language.LanagetItemType.Page);
                    break;
                case "6": e.Row.Cells[5].Text = GetLanageValueByKey("msg_CelueSelectQuest_jiandati", PowerCreator.LMS.Language.LanagetItemType.Page);
                    break;
                case "7": e.Row.Cells[5].Text = GetLanageValueByKey("msg_CelueSelectQuest_zuheti", PowerCreator.LMS.Language.LanagetItemType.Page);
                    break;
         
	        }
            if (e.Row.Cells[9].Text.Trim() == "1")
            {
                e.Row.Cells[9].Text = GetLanageValueByKey("Msg_ManagerQuestion_UnUser", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
            else
            {
                e.Row.Cells[9].Text = GetLanageValueByKey("Msg_ManagerQuestion_IsUser", PowerCreator.LMS.Language.LanagetItemType.Page);
            }

           
        } 
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderGV(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_QuestionIn_Click(object sender, EventArgs e)
    {
        Script.RedirectURL("QuestionToIn.aspx");
    }
    protected void btn_AddQuestion_Click(object sender, EventArgs e)
    {
        Script.RedirectURL("AddQuestionInfo_panduan.aspx");
    }

    protected void btn_AllCheck_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < gvQuestionList.Rows.Count; i++)
        {

            CheckBox objCheckBox = (CheckBox)gvQuestionList.Rows[i].FindControl("Chbox_Check");
            objCheckBox.Checked = true;
        }
    }
    protected void btn_UnCheck_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < gvQuestionList.Rows.Count; i++)
        {

            CheckBox objCheckBox = (CheckBox)gvQuestionList.Rows[i].FindControl("Chbox_Check");
            if (!objCheckBox.Checked)
                objCheckBox.Checked = true;
            else
                objCheckBox.Checked = false;
        }
    }
    protected void btn_DelCheck_Click(object sender, EventArgs e)
    {
        int checkcount = 0;
        for (int i = 0; i < gvQuestionList.Rows.Count; i++)
        {

            CheckBox objCheckBox = (CheckBox)gvQuestionList.Rows[i].FindControl("Chbox_Check");
            if (objCheckBox.Checked)
            {
                checkcount += 1;
                int questionID = int.Parse(gvQuestionList.Rows[i].Cells[2].Text.Trim());
                //检查是否有试卷引用
                //if (!CheckQuestion_Del(questionID))
                //{
                //    return;
                //}
                #region 删除题目表相关记录
                DataTable dt = new DataTable();
                dt = objQuestion.QueryQuestionOption("FKQuestionOptionQuestionID =" + questionID);
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    int OptionID = int.Parse(dt.Rows[j]["QuestionOptionID"].ToString());
                    objQuestion.DeleteQuestionOption(OptionID);
                }
                #endregion

                objQuestion.DeleteQuestion(questionID);
                
            }
        }
        if (checkcount == 0)
        {
            Script.Alert(this, "", GetLanageValueByKey("AddPaper_msg17", PowerCreator.LMS.Language.LanagetItemType.Page));
            return;
        }
        Script.Alert(this, "", GetLanageValueByKey("msg_CelueSelectQuest_shuruti23", PowerCreator.LMS.Language.LanagetItemType.Page));
        string search = GetSearch();
        BinderPager(search);
        BinderGV(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_ChangeQuestionState_Click(object sender, EventArgs e)
    {
        int checkcount = 0;
        for (int i = 0; i < gvQuestionList.Rows.Count; i++)
        {

            CheckBox objCheckBox = (CheckBox)gvQuestionList.Rows[i].FindControl("Chbox_Check");
            if (objCheckBox.Checked)
            {
                checkcount += 1;
                int questionID = int.Parse(gvQuestionList.Rows[i].Cells[2].Text.Trim());
                VOQuestion objVOQuestion = new VOQuestion();
                objVOQuestion = objQuestion.LoadQuestion(questionID);
                objVOQuestion.QuestionState = 2;

                objQuestion.UpdateQuetion(objVOQuestion);

            }
        }
        if (checkcount == 0)
        {
            Script.Alert(this, "", GetLanageValueByKey("AddPaper_msg17", PowerCreator.LMS.Language.LanagetItemType.Page));
            return;
        }
        Script.Alert(this, "", GetLanageValueByKey("msg_CelueSelectQuest_shuruti23", PowerCreator.LMS.Language.LanagetItemType.Page));
        string search = GetSearch();
        BinderPager(search);
        BinderGV(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_UnUserQuestion_Click(object sender, EventArgs e)
    {
        int checkcount = 0;
        for (int i = 0; i < gvQuestionList.Rows.Count; i++)
        {

            CheckBox objCheckBox = (CheckBox)gvQuestionList.Rows[i].FindControl("Chbox_Check");
            if (objCheckBox.Checked)
            {
                checkcount += 1;
                int questionID = int.Parse(gvQuestionList.Rows[i].Cells[2].Text.Trim());
                VOQuestion objVOQuestion = new VOQuestion();
                objVOQuestion = objQuestion.LoadQuestion(questionID);
                objVOQuestion.QuestionState = 1;

                objQuestion.UpdateQuetion(objVOQuestion);

            }
        }
        if (checkcount == 0)
        {
            Script.Alert(this, "", GetLanageValueByKey("AddPaper_msg17", PowerCreator.LMS.Language.LanagetItemType.Page));
            return;
        }
        Script.Alert(this, "", GetLanageValueByKey("msg_CelueSelectQuest_shuruti23", PowerCreator.LMS.Language.LanagetItemType.Page));
        string search = GetSearch();
        BinderPager(search);
        BinderGV(search, AspNetPager1.CurrentPageIndex);
    }
}
